package org.example.mppg.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Package: org.example.mppg.util;
 * @ClassName: RandomDateUtil.java
 * @Description:
 * @author: bo
 * @date: 2024/10/14 10:09
 */

public class DateUtil {

    public static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final String TIME_STR_OF_DEFAULT_FORMAT = " 00:00:00";

    public static LocalDateTime randomLocalDateTime(String startDateStr, String endDateStr) {

        LocalDateTime start = LocalDateTime.parse(startDateStr + TIME_STR_OF_DEFAULT_FORMAT, DEFAULT_FORMATTER);
        LocalDateTime end = LocalDateTime.parse(endDateStr + TIME_STR_OF_DEFAULT_FORMAT, DEFAULT_FORMATTER);

        ZonedDateTime startZonedDateTime = start.atZone(ZoneId.systemDefault());
        ZonedDateTime endZonedDateTime = end.atZone(ZoneId.systemDefault());

        long startTime = startZonedDateTime.toInstant().toEpochMilli();
        long endTime = endZonedDateTime.toInstant().toEpochMilli();

        ThreadLocalRandom random = ThreadLocalRandom.current();
        long randomTime = startTime + (long) (random.nextDouble() * (endTime - startTime));

        return LocalDateTime.ofInstant(Instant.ofEpochMilli(randomTime), ZoneId.systemDefault());
    }
}